package com.yqg.education.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.yqg.education.common.Result;
import com.yqg.education.entity.CourseType;
import com.yqg.education.service.ICourseTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * CourseTypeController 前端控制器
 *
 * @author 张芸博
 * @since 2025-04-10
 */
@RestController
@RequestMapping("/course-type")
public class CourseTypeController {

    @Resource
    private ICourseTypeService courseTypeService;

    /**
     * 新增或者更新
     */
    @PostMapping("/edit")
    public Result save(@RequestBody CourseType courseType) {
        return Result.success(courseTypeService.saveOrUpdate(courseType));
    }

    /**
     * 删除
     */
    @GetMapping("delete/{id}")
    public Result delete(@PathVariable Integer id) {
        CourseType courseType = courseTypeService.getById(id);
        courseType.setIsDelete(1);
        return Result.success(courseTypeService.saveOrUpdate(courseType));
    }

    /**
     * 查询所有
     */
    @GetMapping("/all")
    public Result findAll() {
        QueryWrapper<CourseType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        List<CourseType> list = courseTypeService.list(queryWrapper);
        return Result.success(list);
    }

    /**
     * 根据id查询
     */
    @GetMapping("/{id}")
    public Result findOne(@PathVariable Integer id) {
        return Result.success(courseTypeService.getById(id));
    }

    /**
     * 分页查询
     */
    @GetMapping("/page")
    public Result findPage(@RequestParam Integer pageNum,
                           @RequestParam Integer pageSize,
                           String name) {
        QueryWrapper<CourseType> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("is_delete", 0);
        queryWrapper.like("name", name);
        queryWrapper.orderByDesc("id");
        return Result.success(courseTypeService.page(new Page<>(pageNum, pageSize), queryWrapper));
    }

}

